//
//  MainViewController.m
//  ECBBusinessCamp
//
//  Created by rimi on 16/6/15.
//  Copyright © 2016年 邓毕华. All rights reserved.
//

#import "MainViewController.h"

#import "HomeViewController.h"
#import "SearchBarView.h"

@interface MainViewController ()

@property (nonatomic, strong) SearchBarView * searchBarView; // 搜索栏视图
@property (nonatomic, assign) NSInteger currenIndex; // 当前显示控制器下标

@end

@implementation MainViewController

#pragma mark 🌷🌷🌷life cycle🌷🌷🌷
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 默认显示 首页
    _currenIndex = 0;
    
    // 添加搜索栏
    [self.view insertSubview:self.searchBarView atIndex:999];
    // 添加标签栏
    [self.view insertSubview:self.tabBarView atIndex:999];
    
    // 自动布局
    [_searchBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.sizeOffset(CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT * 0.092));
        make.top.offset(0);
        make.left.offset(0);
    }];
    [_tabBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.sizeOffset(CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT * 0.09));
        make.centerX.equalTo(self.view);
        make.bottom.offset(0);
    }];
}

#pragma mark 🌷🌷🌷touches🌷🌷🌷
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

#pragma mark 🌷🌷🌷getters and setters🌷🌷🌷
- (SearchBarView *)searchBarView {
    if (!_searchBarView) {
        _searchBarView = [[SearchBarView alloc] initWithSearchStr:^(NSString *searchStr) {
            NSLog(@"%@", searchStr);
        }];
    }
    return _searchBarView;
}
- (DBHTabBarView *)tabBarView {
    if (!_tabBarView) {
        NSArray * vcNames = @[@"HomeViewController", @"TrainingCourseViewController", @"CourseTeacherViewController", @"CourseForecastViewController", @"StudentsActicitiesViewController", @"StudentsEnjoyableViewController", @"CommunityInteractionViewController", @"PersonalHomePageViewController"];
        NSArray * tabBarNames = @[@"首页", @"培训课程", @"课程师资", @"课程预告", @"学员活动", @"同学尊享", @"互动社区", @"个人主页"];
        _tabBarView = [[DBHTabBarView alloc] initWithNames:tabBarNames selectIndex:^(NSInteger selecIndex) {
            // 是否重复选中 重复选中不做任务操作
            if (selecIndex == _currenIndex) {
                return;
            }
            // 动画进行时关闭用户交互
            _tabBarView.userInteractionEnabled = NO;
            NSArray * subviews = self.view.subviews;
            // 获取到最上层显示的视图及其控制器
            UIView * topView = subviews[subviews.count - 3];
            UIViewController * topVc = self.childViewControllers.lastObject;
            // 添加视图与控制器
            UIViewController * vc = selecIndex == 3 || selecIndex == 4 ? [self.storyboard instantiateViewControllerWithIdentifier:vcNames[selecIndex]] : [[NSClassFromString(vcNames[selecIndex]) alloc] init];
            vc.view.frame = CGRectMake(selecIndex > _currenIndex ? SCREEN_WIDTH * 0.3 : SCREEN_WIDTH * - 0.3, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
            [self addChildViewController:vc];
            [self.view insertSubview:vc.view belowSubview:topView];
            [vc didMoveToParentViewController:self];
            
            // 动画
            [UIView animateWithDuration:1.0 animations:^{
                topView.center = CGPointMake(selecIndex > _currenIndex ? -SCREEN_WIDTH * 0.5 : SCREEN_WIDTH * 1.5, SCREEN_HEIGHT * 0.5);
                vc.view.center = CGPointMake(SCREEN_WIDTH * 0.5, SCREEN_HEIGHT * 0.5);
            } completion:^(BOOL finished) {
                // 动画完成后开启用户交互
                _tabBarView.userInteractionEnabled = YES;
                
                // 移除视图和控制器
                [topView removeFromSuperview];
                [topVc removeFromParentViewController];
            }];
            // 更新 当前显示控制器下标
            _currenIndex = selecIndex;
        }];
        _tabBarView.backgroundColor = RGB_COLOR(70, 70, 70, 1);
    }
    return _tabBarView;
}

@end
